//
//  KYCustomShareView.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/21.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYCustomShareView.h"
#import "KYCustomButton.h"

@interface KYCustomShareView()
/**
 *  背景
 */
@property (nonatomic, weak) UIView *bgView;
/**
 *  展现内容的view
 */
@property (nonatomic, weak) UIView *contentView;
/**
 *  分享标题
 */
@property (nonatomic, weak) UILabel *titleLabel;
/**
 *  分享按钮的数组
 */
@property (nonatomic, strong) NSMutableArray *btnsArr;
/**
 *  分享按钮
 */
@property (nonatomic, weak) KYCustomButton *shareToFriend;
@property (nonatomic, weak) KYCustomButton *shareToKongZone;
@property (nonatomic, weak) KYCustomButton *shareToWxFriend;
@property (nonatomic, weak) KYCustomButton *shareToWxZone;
@property (nonatomic, weak) KYCustomButton *shareToQQ;
@property (nonatomic, weak) KYCustomButton *shareToQQZone;
@property (nonatomic, weak) KYCustomButton *shareToSina;

@end

@implementation KYCustomShareView

- (NSMutableArray *)btnsArr
{
    if (_btnsArr == nil) {
        _btnsArr = [NSMutableArray array];
    }
    return _btnsArr;
}

+ (instancetype)customShareView
{
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setUpSubViews];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)decoder
{
    if (self = [super initWithCoder:decoder]) {
        
        [self setUpSubViews];
    }
    return self;
}

#pragma mark - setUp初始化
- (void)setUpSubViews
{
    // 0.初始化子控件
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0.65;
    [self addSubview:bgView];
    self.bgView = bgView;
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:contentView];
    self.contentView = contentView;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = NSLocalizedString(@"分享到", nil);
    titleLabel.textColor = KYColor(83, 83, 88, 1.0);
    titleLabel.font = [UIFont systemFontOfSize:14.5f];
    [contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    [self setUpBtnWithTitle:NSLocalizedString(@"好友", nil) normalImg:@"me_share_friendlist_normal" tag:KYCustomShareViewButtonShareToFriendType];
    [self setUpBtnWithTitle:NSLocalizedString(@"快眼友圈", nil) normalImg:@"me_share_status_normal" tag:KYCustomShareViewButtonShareToKongZoneType];
    [self setUpBtnWithTitle:NSLocalizedString(@"微信好友", nil) normalImg:@"me_share_wechat_normal" tag:KYCustomShareViewButtonShareToWxFriendType];
    [self setUpBtnWithTitle:NSLocalizedString(@"朋友圈", nil) normalImg:@"me_share_moment_normal" tag:KYCustomShareViewButtonShareToWxZoneType];
    [self setUpBtnWithTitle:NSLocalizedString(@"QQ", nil) normalImg:@"me_share_qq_normal" tag:KYCustomShareViewButtonShareToQQType];
    [self setUpBtnWithTitle:NSLocalizedString(@"QQ空间", nil) normalImg:@"me_share_qqzone_normal" tag:KYCustomShareViewButtonShareToQQZoneType];
    [self setUpBtnWithTitle:NSLocalizedString(@"新浪微博", nil) normalImg:@"me_share_weibo_normal" tag:KYCustomShareViewButtonShareToSinaType];
    
    // 1.添加手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(triggerTapGesture:)];
    [bgView addGestureRecognizer:tapGesture];
}

/**
 *  创建按钮
 *
 *  @param title     标题
 *  @param normalImg 普通状态的图片
 *  @param tag       标识
 */
- (void)setUpBtnWithTitle:(NSString *)title normalImg:(NSString *)normalImg tag:(NSInteger)tag
{
    KYCustomButton *btn = [[KYCustomButton alloc] init];
    btn.titleLabel.font = [UIFont systemFontOfSize:13.5f];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:KYColor(83, 83, 88, 1.0) forState:UIControlStateNormal];
    [btn setImage:[UIImage imageWithName:normalImg] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    btn.tag = tag;
    [self.contentView addSubview:btn];
    [self.btnsArr addObject:btn];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.bgView.frame = self.bounds;
    
    CGFloat contentViewX = 0;
    CGFloat contentViewH = 215;
    CGFloat contentViewY = self.frame.size.height - contentViewH;
    CGFloat contentViewW = self.frame.size.width;
    self.contentView.frame = CGRectMake(contentViewX, contentViewY, contentViewW, contentViewH);
    
    CGFloat titleLabelX = 20;
    CGFloat titleLabelY = 10;
    CGFloat titleLabelW = contentViewW;
    CGFloat titleLabelH = 20;
    self.titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
    
    CGFloat margin = 10;
    CGFloat btnW = (self.frame.size.width - 2 * titleLabelX - 4 * margin) / 5;
    CGFloat btnH = 60;
    for (int i = 0; i < self.btnsArr.count; i++) {
        KYCustomButton *btn = self.btnsArr[i];
        CGFloat btnX = titleLabelX + i * (btnW + margin);
        CGFloat btnY = CGRectGetMaxY(self.titleLabel.frame) + 15;
        if (i > 1) {
            btnX = titleLabelX + (i - 2) * (btnW + margin);
            btnY = 10 + 20 + 15 + 60 + 30;
        }
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
}

#pragma mark - gesture
/**
 *  点击弹出框以外的部分, 则收起当前弹出框
 *
 *  @param gestureRecognizer UITapGestureRecognizer
 */
- (void)triggerTapGesture:(UITapGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        [self hide];
    }
}

#pragma mark - 按钮点击
- (void)buttonClick:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(customShareView:didClickShareBtnType:)]) {
        
        // 0.关闭当前页面
        [self hide];
        
        // 1.通知代理
        [self.delegate customShareView:self didClickShareBtnType:button.tag];
    }
}

#pragma mark - public
/**
 *  显示可连接设备View
 */
- (void)show {
    UIWindow *appWindow = [[[UIApplication sharedApplication] delegate] window];
    [appWindow addSubview:self];
}

/**
 *  隐藏可连接设备View
 */
- (void)hide {
    [self removeFromSuperview];
}


@end
